﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;

using MySql.Data.MySqlClient;
using Think.Lenovo.Com.Models;
using Think.Lenovo.Com.Common;

namespace Think.Lenovo.Com.DataAccess
{
    public class Power
    {

        /// <summary>
        /// 根扰权限id 得到一个权限实体
        /// </summary>
        /// <param name="id">权限id</param>
        /// <returns></returns>
        public PowerModel GetPower(int id)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder conditions = new StringBuilder(60);

            StringBuilder sql = new StringBuilder(512);
            sql.Append("Select * from tbl_power Where id=" + id);
            DataSet ds = MysqlHelper.Query(conn, sql.ToString());
            PowerModel model = new PowerModel();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    model.Id = Convert.ToInt32(ds.Tables[0].Rows[0]["Id"]);
                    model.Code = Convert.ToString(ds.Tables[0].Rows[0]["Code"]);
                    model.Name = Convert.ToString(ds.Tables[0].Rows[0]["Name"]);
                    model.Description = Convert.ToString(ds.Tables[0].Rows[0]["Description"]);
                }
            }
            return model;

        }

        public PowerModel GetPower(String code)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder conditions = new StringBuilder(60);

            StringBuilder sql = new StringBuilder(512);
            sql.Append("Select * from tbl_power Where id=" + code);
            DataSet ds = MysqlHelper.Query(conn, sql.ToString());
            PowerModel model = new PowerModel();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    model.Id = Convert.ToInt32(ds.Tables[0].Rows[0]["Id"]);
                    model.Code = Convert.ToString(ds.Tables[0].Rows[0]["Code"]);
                    model.Name = Convert.ToString(ds.Tables[0].Rows[0]["Name"]);
                    model.Description = Convert.ToString(ds.Tables[0].Rows[0]["Description"]);
                }
            }
            return model;
        }

        /// <summary>
        /// 根据权限编号得到权限实体集合
        /// </summary>
        /// <param name="powerCodes">权限编码</param>
        /// <returns>LinkedList<PowerModel></returns>
        public LinkedList<PowerModel> GetPower(IEnumerable<String> powerCodes)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder conditions = new StringBuilder(200);
            IEnumerator<String> ie = powerCodes.GetEnumerator();
            while (ie.MoveNext())
            {
                conditions.Append(ie.Current + ",");
            }
            StringBuilder sql = new StringBuilder(512);
            sql.Append("Select * from tbl_power ");
            if (conditions.Length > 0)
            {
                conditions.Remove(conditions.Length - 1, 1);//移除最后一个逗号
                sql.Append(" Where code in(")
                    .Append(conditions)
                    .Append(")");
            }
            DataSet ds = MysqlHelper.Query(conn, sql.ToString());
            LinkedList<PowerModel> list = new LinkedList<PowerModel>();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        PowerModel model = new PowerModel();
                        model.Id = Convert.ToInt32(row["Id"]);
                        model.Code = Convert.ToString(row["Code"]);
                        model.Name = Convert.ToString(row["Name"]);
                        model.Description = Convert.ToString(row["Description"]);
                        list.AddLast(model);
                    }
                }
            }
            return list;
        }
        /// <summary>
        /// 根据指定的权限id,得到相应的权限
        /// </summary>
        /// <param name="ids">权限id集合</param>
        /// <returns>LinkedList<PowerModel> </returns>
        public LinkedList<PowerModel> GetPower(IEnumerable<int> ids)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            StringBuilder sql = new StringBuilder(512);
            sql.Append("Select * from tbl_power ");
            LinkedList<PowerModel> list = new LinkedList<PowerModel>();
            StringBuilder conditions = new StringBuilder(60);
            IEnumerator<int> ie = ids.GetEnumerator();
            while (ie.MoveNext())
            {
                conditions.Append(ie.Current + ",");
            }

            if (conditions.Length > 0)
            {
                conditions.Remove(conditions.Length - 1, 1);//移除最后一个逗号
                sql.Append(" Where id in(")
                    .Append(conditions)
                    .Append(")");
                DataSet ds = MysqlHelper.Query(conn, sql.ToString());
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            PowerModel model = new PowerModel();
                            model.Id = Convert.ToInt32(row["Id"]);
                            model.Code = Convert.ToString(row["Code"]);
                            model.Name = Convert.ToString(row["Name"]);
                            model.Description = Convert.ToString(row["Description"]);
                            list.AddLast(model);
                        }
                    }
                }
            }
            return list;
        }
        /// <summary>
        /// 得到本系统中所有的权限
        /// </summary>
        /// <returns>LinkedList<PowerModel> </returns>
        public LinkedList<PowerModel> GetAllPower()
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(512);
            sql.Append("Select * from tbl_power order by code");
            DataSet ds = MysqlHelper.Query(conn, sql.ToString());
            LinkedList<PowerModel> list = new LinkedList<PowerModel>();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        PowerModel model = new PowerModel();
                        model.Id = Convert.ToInt32(row["Id"]);
                        model.Code = Convert.ToString(row["Code"]);
                        model.Name = Convert.ToString(row["Name"]);
                        model.Description = Convert.ToString(row["Description"]);
                        list.AddLast(model);
                    }
                }
            }
            return list;
        }

        public long AddPower(PowerModel model)
        {

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            //sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("insert into ")
                .Append("tbl_power (Code, Name, Description)")
                .Append("values(@Code,@Name,@Description)");


            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@Code",MySqlDbType.VarChar,50),
                    new MySqlParameter("@Name",MySqlDbType.VarChar,32),
                    new MySqlParameter("@Description",MySqlDbType.VarChar,50)
                };

            ps[0].Value = model.Code;
            ps[1].Value = model.Name;
            ps[2].Value = model.Description;

            long id = MysqlHelper.Insert(conn, sql.ToString(), ps);
            return id;

        }
    }
}
